import request from "@/tools/request";

export const fenYe=(pageIndex,pageSize) =>{
    return request.get("http://localhost:8081/user/page?pageIndex="+pageIndex+"&pageSize="+pageSize).then(res=>{
        // console.log(res)
        res.data.findUser = res.data.findUser.map(item => {
        const createdDate = new Date(item.create_time);
        const formattedDateTime = createdDate.toLocaleString();
        return {
          ...item,
          create_time: formattedDateTime,
          update_time:formattedDateTime
        };
      });
      return res
    })
}


export const searchBySome=(username,email,address)=>{
  return request.get("/user/search",{params:{
    "username":username,
    "email":email,
    "address":address
  }}
  ).then(res=>{
    if(res.data.total>0){
    res.data.findUser = res.data.findUser.map(item => {
      const createdDate = new Date(item.create_time);
      const formattedDateTime = createdDate.toLocaleString();
      return {
        ...item,
        create_time: formattedDateTime,
        update_time:formattedDateTime
      };
    });}
    console.log(res)
    return res.data
  })
}



export const addUSer=(user)=>{
  return request.post("/user/addUser",user)
}

export const updateUser=(user)=>{
  return request.post("/user/updateUser",user)
}


export const delUser=(id)=>{
  return request.get("/user/delUser",{
    params:{
      "id":id
    }
  })
}

export const delUsers=(ids)=>{
  const encodedIds = encodeURIComponent(ids.join());
  return request.get(`/user/DelUsers?ids=${encodedIds}`)
}

export const exportExcel = () => {
  return request({
    method: 'get',
    url: '/user/export',
    responseType: 'blob'
  })
    .then(response => {
      const url = window.URL.createObjectURL(new Blob([response.data]));
      const link = document.createElement('a');
      link.href = url;
      link.setAttribute('download', 'users.xls');
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    })
    .catch(error => {
      console.error(error);
    });
};

